package com.xhj.controller;

import com.xhj.entity.Member;
import com.xhj.service.MemberService;
import com.xhj.util.JwtUtil;
import com.xhj.util.ResultEntity;
import com.xhj.vo.LoginVo;
import com.xhj.vo.MemberVo;
import com.xhj.vo.RegisterVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 会员表(UcenterMember)表控制层
 *
 * @author makejava
 * @since 2021-03-08 14:42:27
 */
@CrossOrigin
@RestController
@RequestMapping("/member/")
public class MemberController {

    @Autowired
    private MemberService memberService;

    @ApiOperation(value = "会员注册")
    @RequestMapping("register")
    public ResultEntity register(@RequestBody RegisterVo registerVo
            ){
        memberService.register(registerVo);
        return ResultEntity.successWithoutData();
    }

    @ApiOperation(value = "会员登录")
    @RequestMapping("login")
    public ResultEntity login(@RequestBody LoginVo loginVo){
        String token = memberService.login(loginVo);
        return ResultEntity.successWithData().data("token",token);
    }

    @ApiOperation(value = "根据token获取登录信息")
    @RequestMapping("auth/getLoginInfo")
    public ResultEntity getLoginInfo(HttpServletRequest request){
        try {
            String memberId = JwtUtil.getMemberIdByJwtToken(request);
            LoginVo loginVo = memberService.getLoginInfo(memberId);
            return ResultEntity.successWithData().data("loginVo",loginVo);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

    @ApiOperation(value = "根据id获取用户信息")
    @RequestMapping("getInfo/{id}")
    public MemberVo getMemberInfoById(@PathVariable String id){
        Member member = memberService.getById(id);
        MemberVo memberVo = new MemberVo();
        BeanUtils.copyProperties(member,memberVo);
        return memberVo;
    }

    @ApiOperation(value = "统计一天的注册人数")
    @RequestMapping("countRegister/{day}")
    public Integer countRegister(@PathVariable("day")String day){
        int count = memberService.countRegister(day);
        return count;
    }
}